package com.gao.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author gao
 * @time 2020/05/29 21:30:41
 */
@Controller
@RequestMapping("auth")
public class AuthController {

    @PostMapping("login")
    public String login(String username, String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        String path = null;
        try {
            subject.login(token);
            path = "home";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            path = "login";
        }
        return "redirect:/view/" +path;
    }
}
